import {createRouter,createWebHashHistory} from 'vue-router'

const router =createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:'/',
            component:()=>import('../views/home/Home.vue')
        },
        {
            path:'/home',
            component:()=>import('../views/home/Home.vue')
        },
        {
            path:'/cart',
            component:()=>import('../views/cart/Cart.vue'),
            meta:{
                isAuth:true
            }
          
        },
        {
            path:'/my',
            component:()=>import('../views/my/My.vue'),
            meta:{
                isAuth:true
            }
        },
        {
            path:'/order',
            component:()=>import('../views/order/Order.vue'),
            meta:{
                isAuth:true
            }
        },
        {
            path:'/login',
            component:()=>import('../views/login/Login.vue')
        },
        {
            path:'/register',
            component:()=>import('../views/register/Register.vue')
        },
        {
            path:'/userInfoEdit',
            component:()=>import('../views/userInfoEdit/UserInfoEdit.vue')
        },
        {
            path:'/address',
            component:()=>import('../views/address/Address.vue')
        },
        {
            path:'/addressEdit',
            component:()=>import('../views/address/AddressEdit.vue')
        },
        {
            path:'/store',
            component:()=>import('../views/myStore/MyStore.vue')
        }
    ]
})


router.beforeEach((to,from,next)=>{
    console.log('Login:',localStorage.getItem("isLogin"))
    if(to.meta.isAuth){  //需要登录验证
        if(localStorage.getItem("isLogin") == '1' ) { //1表示已登录  
            next()
        }else{
            alert('请先登录')
            router.push('./login')
        }
    }else{ //不需要登录验证
        next()
    }
})


















// router.beforeEach((to,from,next)=>{
//     if(to.meta.isAuth){
//         if(localStorage.isLogin == '1'){
//             next()
//         }else{
//             alert('请先登录！')
//             router.push('./login')
//         }
//     }else{
//         next()
//     }
// })

export default router